package com.dou.controller;

import com.alibaba.fastjson.JSONObject;
import com.dou.dto.Constant;
import com.dou.dto.Dto;
import com.dou.dto.DtoUtil;
import com.dou.dto.RedisUtil;
import com.dou.entity.QiangGou;
import com.dou.servier.impl.SeckillGoodsService;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.jms.listener.endpoint.JmsActivationSpecConfig;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("seckill")
public class SeckillController {
    @Autowired
    SeckillGoodsService seckillGoodsService;
    @Autowired
    JmsMessagingTemplate jmsMessagingTemplate;
    @Autowired
    private RedisUtil redisUtil;
    @PostMapping("qiangGou")
    public Dto qiangGou(String tonken,Integer sId,Integer gId){
        return seckillGoodsService.seckill(tonken, sId, gId);
    }
    @PostMapping("qiangGou2")
    public Dto qiangGou2(QiangGou gg){
        String uJson = redisUtil.getStr(gg.getTonken());
        if(uJson == null || uJson.equals("")){
            return DtoUtil.returnFail("用户未登陆","4001");
        }
        gg.setUid(JSONObject.parseObject(uJson).getInteger("id"));
        //1.针对消息队列进行封装，封装成工具类
        //2.记录下发送消息的状态
        jmsMessagingTemplate
                .convertAndSend(new ActiveMQQueue(Constant.MQ.QIANGGOU)
                ,gg);//消息 用于传输数据 需要将tonken sid gid都发过去
        return DtoUtil.returnSuccess();
    }
    @GetMapping("que")
    public void que(){
        //发送点对点消息
        jmsMessagingTemplate.convertAndSend(new ActiveMQQueue("que"),"点对点");
    }
    @GetMapping("ding")
    public void ding(){
        jmsMessagingTemplate.convertAndSend(new ActiveMQTopic("ding"),"这里是一对多订阅");
    }
    @JmsListener(destination = "ding")
    public void dingtalk(String ding){
        System.out.println(ding);
    }



    //对外提供接口 查询redis存入的状态
    //由前端循环查询 知道查询结果
}
